<?php

namespace app\adminapi\controller;

use think\facade\Db;

class Role extends Common
{
    public function list()
    {
        $data = $this->_vali([
            'keyword.default' => '',
            'doctimeStart.default' => '',
            'doctimeEnd.default' => '',
        ],"POST");

        $where = [];

        if ($data['keyword']) {
            $where[] = ['role_name','like',"%".$data['keyword']."%"];
        }

        $list = Db::name('role')->field('id,role_name,status')->where($where)->paginate(10);

        $list = $list->toArray();

        return json(['status'=>200,'sum'=>$list['total'],'list'=>$list['data']]);
    }

    /**
     * 添加/编辑
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save()
    {
        $data = $this->_vali([
            'id.default' => '',
            'role_name.require' => '请输角色名',
        ],"POST");

        if ($data['id']) {
            if (Db::name('role')->where('id','<>',$data['id'])->where('role_name',$data['role_name'])->find()) {
                return json(['code'=>0,'msg'=>'角色名称已存在']);
            }
            Db::name('role')->where('id',$data['id'])->update($data);
        } else {
            if (Db::name('role')->where('role_name',$data['role_name'])->find()) {
                return json(['code'=>0,'msg'=>'角色名称已存在']);
            }
            $data['create_time'] = time();
            if (Db::name('role')->insert($data)) {
                return json(['code'=>200,'msg'=>'操作成功']);
            } else {
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }

        return json(['code'=>200,'msg'=>'操作成功']);
    }

    /**
     * 获取详情
     * @return \think\response\Json
     */
    public function get()
    {
        $data = $this->_vali([
            'id.require' => 'id不能为空',
        ],"POST");

        $data = Db::name('role')->where('id',$data['id'])->field('id,role_name')->find();

        return json(['code'=>200,'data'=>$data]);
    }

    /**
     * 设置状态
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function status()
    {
        $data = $this->_vali([
            'id.require' => 'id不能为空',
            'status.require' => '请输入商户简称',
        ],"POST");

        $res = Db::name('role')->where('id',$data['id'])->update($data);
        if ($res) {
            return json(['code'=>200,'msg'=>'操作成功']);
        } else {
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function del()
    {
        $data = $this->_vali([
            'ids.require' => 'ids不能为空',
        ],"POST");

        if (Db::name('role')->where('id','in',$data['ids'])->delete()) {
            return json(['code'=>200,'data'=>'操作成功']);
        } else {
            return json(['code'=>200,'data'=>'操作失败']);
        }
    }

     /**
     * 获取授权内容
     * @return \think\response\Json
     */
    public function author()
    {
        $data = $this->_vali([
            'id.require' => 'id不能为空',
        ],"POST");

        $list = Db::name('menu')->field('id,title as name,pid')->select();

        $map = $this->menu($list, 0);

        $checkedKeys = Db::name('role')->where('id',$data['id'])->value('rule_list');

        if ($checkedKeys) {
            $checkedKeys = explode(",",$checkedKeys);

            foreach ($checkedKeys as $key => $value) {
                $checkedKeys[$key] = (int) $value;
            }
        } else {
            $checkedKeys = [];
        }

        return json(['code'=>200,'list'=>$map,'checkedKeys'=>$checkedKeys]);
    }

    /**
     * 组装数据
     * @param $data
     * @param $pid
     * @return array
     */
    public function menu($data,$pid) {
        $ret = array();
        foreach ($data as $k => $v) {
            if($v['pid'] == $pid) {
                $map['key'] = $v['id'];
                $map['title'] = $v['name'];
                if ($this->menu($data,$v['id'])) {
                    $map['children'] = $this->menu($data,$v['id']);
                }

                $ret[] = $map;
                unset($map);
            }
        }
        return $ret;
    }

    /**
     * 保存授权
     * @return \think\response\Json
     */
    public function save_rule()
    {
        $data = $this->_vali([
            'id.require' => 'id不能为空',
            'rule_list.require' => '请选择商户授权模块',
            'rule_list.array' => '商户授权模块必须是数组',
        ],"POST");

        $rule_list = implode(",",$data['rule_list']);

        if (Db::name('role')->where('id',$data['id'])->find()) {
            Db::name('role')->where('id',$data['id'])->update(['rule_list'=>$rule_list]);
        } else {
            Db::name('role')->insert(['id'=>$data['id'],'rule_list'=>$rule_list]);
        }
        return json(['code'=>200,'msg'=>'操作成功']);
    }

}